package DomainLayer;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class test
{
    private Person[] samplePerson;
    private DVD[] sampleDVD;
    private Loan[] sampleLoans;
    
    private AddressBook addrbook;

    private Person existingPerson;
    private Person newPerson;
    private Person editedPerson;
    private DVD dVD1;
    private DVD dVD2;
    private Copy copy1;
    private Copy copy2;
    private Loan loan1;
    
    
    
    
    /**
     * Default constructor for test class test
     */
    public test()
    {
                addrbook = new AddressBook();
        dVD2 = new DVD(1, "testunit", "lqlq");
        copy2 = new Copy(dVD2);
        samplePerson = new Person[] {
            new Person(1,"george",  "address 1", 1000, "city 1", "phone 1"),
            new Person(2,"peter", "address 2", 2000, "city 2", "phone 2"),
            new Person(3,"chang", "address 3", 3000, "city 3", "phone 3"),
            new Person(4,"browly", "address 4", 4000, "city 4", "phone 4"),
        };
        
         sampleDVD = new DVD[] {
            new DVD(1,"asd1", "asd2"),
            new DVD(2,"asd3", "asd4"),
        };
        
        sampleLoans = new Loan[] {
            new Loan(1, "12.12.12", 2, Loan.State.BORROWED, copy2),
            new Loan(2, "12.12.12", 2, Loan.State.BORROWED, copy2),
        };
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {
        addrbook = new AddressBook();
        
        for(Person info : samplePerson) {
            addrbook.addPerson(info);
        }
        
         for(DVD info : sampleDVD) {
            addrbook.addDVD(info);
        }
        
       
        Person first = samplePerson[0]; 
        existingPerson = new Person(first.getId(), first.getName(), first.getAddress(), first.getPostalCode(), first.getCity(), first.getPhone());
        newPerson = new Person(5, "nikola", "vesterbro", 9000, "aalborg", "8146657");
        editedPerson = new Person((first.getId()+1), first.getName(), first.getAddress() + "X", first.getPostalCode(), first.getCity(), first.getPhone());

        for(Loan info : sampleLoans) {
            addrbook.addLoan(first, "", 1, copy1);
        }
        

    }

    @After
    public void tearDown()
    {
        addrbook = null;
    }
    
   
    
    //addCopy
    @Test
    public void testAddCopy()
    {
        Copy copy1 = new Copy(dVD1);
        addrbook.addCopy(dVD1, "123456", "12/12/12", 129.99);
        assertEquals(dVD1, copy1.getOriginal());
       
    }
    
    //addLoan
    
    @Test
    public void testAddLoan() 
    {
        assertEquals(0, newPerson.getLoans().size());
        addrbook.addLoan(newPerson, "12/12/12", 2, copy1);
        assertEquals(1, newPerson.getLoans().size());
    }
    
    //addPerson 
    @Test
    public void testAddPerson()
    {
        assertNull(addrbook.getPerson(newPerson.getName()));
        addrbook.addPerson(newPerson);
        assertEquals(samplePerson.length + 1, addrbook.getPeople().size());
    }
    
    //editCopy
    
    @Test
    public void testEditCopy()  
    {   
        DVD dVD1 = new DVD(1, "Title", "12/12/12");      
        Copy copy1 = new Copy(dVD1);
        addrbook.editCopy(copy1, "654321", "12/04/2000", 111.99);
        assertEquals("654321", copy1.getSerialNum());
        assertEquals("12/04/2000", copy1.getPurchaseDate());
        assertEquals(111.99, copy1.getPurchasePrice(), 0.1);
    }
    
    //editDVD
        
    @Test
    public void testEditDVD()
    {
           DVD dVD1 = new DVD(1, "Title", "12/12/12");
           addrbook.editDVD(dVD1, "Jaws", "13/11/2010");
           assertEquals("Jaws", dVD1.getTitle());
           assertEquals("13/11/2010", dVD1.getPublishDate());
    }
    
    //editPerson
    
    @Test 
    public void testEditPerson() 
    {
        addrbook.editPerson(existingPerson, "Daniel", "Somewhere", "Copenhagen", 2000, "1111111");
        assertEquals("Daniel", existingPerson.getName());
        assertEquals("Somewhere", existingPerson.getAddress());
        assertEquals("Copenhagen", existingPerson.getCity());
        assertEquals(2000, existingPerson.getPostalCode());
        assertEquals("1111111", existingPerson.getPhone ());
    }
    
    //removePerson 
    @Test
    public void testRemovePerson()
    {
        addrbook.addPerson(newPerson);
        assertEquals(samplePerson.length + 1, addrbook.getPeople().size());
        addrbook.removePerson(newPerson);
        assertEquals(samplePerson.length, addrbook.getPeople().size());        
    }
    
    //getPeople
    @Test
    public void testGetPeople()
    {
        assertEquals(samplePerson.length, addrbook.getPeople().size());
    }
    
    //getDVDs
      @Test
    public void testGetDVDs()
    {
        assertEquals(sampleDVD.length, addrbook.getDVDs().size());
    }
    
    //removeDVD
    @Test
    public void testRemoveDVD()
    {
        DVD asd = new DVD(1, "", "");
        addrbook.addDVD(asd);
        assertEquals(sampleDVD.length + 1, addrbook.getDVDs().size());
      addrbook.removeDVD(asd);
      assertEquals(sampleDVD.length, addrbook.getDVDs().size());        
    }
}








